home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdatabs / as2ps.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-31  |  13.1 KB  |  419 lines

  1. // ------------------------------- //
  2. // -------- Start of File -------- //
  3. // ------------------------------- //
  4. // ----------------------------------------------------------- // 
  5. // C++ Source Code File Name: as2ps.cpp
  6. // Compiler Used: MSVC40, DJGPP 2.7.2.1, GCC 2.7.2.1, HP CPP 10.24
  7. // Produced By: Doug Gaer 
  8. // File Creation Date: 12/17/1997
  9. // Date Last Modified: 03/31/1999
  10. // ----------------------------------------------------------- // 
  11. // ------------- Program Description and Details ------------- // 
  12. // ----------------------------------------------------------- // 
  13. /*
  14. ASCII to PostScript converter program.
  15. */
  16. // ----------------------------------------------------------- // 
  17. #include <iostream.h>
  18. #include <fstream.h>
  19. #include <string.h>
  20. #include <stdlib.h> 
  21. #include <stdio.h>
  22. #include "as2ps.h"
  23.  
  24. // AS2PS program globals
  25. int VerboseMode = 0;     // Echo results (off by default)
  26. char out_file[MAX_LINE]; // PostScript file created from text file
  27. char time_buf[255];      // Buffer used to hold time/date string
  28. char *open_file;         // File currently open
  29. int write_to_file = 0;   // Write the output to a file instead of stdout
  30. unsigned num_files = 0;   // Total number of files processed
  31.  
  32. // Setup as2ps default values
  33. PostScriptDrv::PSFonts font_code = PostScriptDrv::COURIER; 
  34. PostScriptDrv::PSPaperSizes paper_code = PostScriptDrv::LETTER_SIZE; 
  35. double font_size = DEFAULT_POINT_SIZE; // Font or pitch size
  36. double lr_margin = DEFAULT_LR_MARGIN;  // Left and Right margins
  37. double tb_margin = DEFAULT_TB_MARGIN;  // Top and Bottom margins 
  38. int man_page = 0;   // Format UNIX man page
  39. int cut_lines = 1;  // Truncate lines to match the number of columns
  40. int wrap_lines = 0; // Turn line wrap on or off
  41. int duplex = 0;     // Enable duplex printing
  42. int manualfeed = 0; // Print from manual feed position
  43. int use_tray = 0;   // Use a specified paper tray
  44. int tray_number;    // Tray number for the paper tray
  45.  
  46. void HelpMessage(const char *program_name, const double version_number)
  47. {
  48.   char vbuffer[255];
  49.   sprintf(vbuffer, "%.3f", version_number);
  50.   cout << endl;
  51.   cout << "ASCII to PostScript Converter version " << vbuffer << endl;
  52.   cout << "Usage: " << program_name << " [switches] infile.txt " << endl;
  53.   cout << "Switches: -?      = Display this help message." << endl;
  54.   cout << "          -c#     = Number of copies to print. Example: -c2"
  55.        << endl;
  56.   cout << "          -d      = Enable duplex printing if available." << endl;
  57.   cout << "          -D      = Print from manual feed position." << endl;
  58.   cout << "          -fcode  = Select a font type. Example: -fC" << endl;
  59.   cout << "          -F      = Write output to a file: infile.txt = infile.ps"
  60.        << endl;
  61.   cout << "          -h      = Use page headers and numbers."
  62.        << endl;
  63.   cout << "          -H      = Use page headers and numbers with lines."
  64.        << endl;
  65.   cout << "          -l      = Print in landscape mode instead of portrait."
  66.        << endl;
  67.   cout << "          -m#     = Set left and right margins. Example: -m1.0"
  68.        << endl;
  69.   cout << "          -M#     = Set top and bottom margins. Example: -M1.0"
  70.        << endl;
  71.   cout << "          -pcode  = Select a paper size. Example: -pLET" << endl;
  72.   cout << "          -s#     = Specify font size. Example: -s10.0" << endl;
  73.   cout << "          -t#     = Spaces between tab stops. Example: -t8"
  74.        << endl;
  75.   cout << "          -T      = Do not truncate lines that exceed margin."
  76.        << endl;
  77.   cout << "          -u      = Format UNIX man pages produced by nroff."
  78.        << endl;
  79.   cout << "          -v      = Turn verbose mode on." << endl;
  80.   cout << "          -W      = Turn line wrap on." << endl;
  81.   cout << "          -y#     = Use a specified paper tray. Example: -y1"
  82.        << endl;
  83.   cout << endl;
  84.   cout << "Press enter to continue..." << endl;
  85.   cin.get();
  86.  
  87.   cout << "Valid font codes for -f option: " << endl;
  88.   cout << endl;
  89.   cout << "\tC - Courier, CB - Courier-Bold, CO - Courier-Oblique,"  << endl;
  90.   cout << "\tCBO - Courier-BoldObligue, T - Times-Roman, TB - Times-Bold"
  91.        << endl;
  92.   cout << "\tTI - Times-Italic, TBI - Times-BoldItalic, H - Helvetica" << endl;
  93.   cout << "\tHBO - Helvetica-BoldOblique, HO - Helvetica-Oblique, " << endl;
  94.   cout << "\tHB - Helvetica-Bold,  S - Symbol" << endl;
  95.  
  96.   cout << endl;
  97.   cout << "Valid paper codes for -p option: " << endl;
  98.   cout << endl;
  99.   cout << "\tLET - Letter size  8.5 x 11 inches" << endl;
  100.   cout << "\tLEG - Legal size   8.5 x 14 inches" << endl;
  101.   cout << "\tTAB - Tabloid size 11 x 17 inches" << endl;
  102.   cout << "\tA3  - A3 size 297mm x 420mm (11.70 x 16.55 inches)" << endl;
  103.   cout << "\tA4  - A4 size 210mm x 297mm (8.27 x 11.70 inches)" << endl;
  104.   exit(0);
  105. }
  106.  
  107. // Process Program argument list
  108. PostScriptDrv ProcessArgs(int argc, char *argv[])
  109. {
  110.   // Establish default values
  111.   PostScriptDrv psdrv;
  112.   int ibuf;
  113.   char *sbuf;
  114.   
  115.   // process the program's argument list
  116.   int i;
  117.   for(i = 1; i < argc; i++ ) {
  118.     if(*argv[i] == '-') {
  119.       char sw = *(argv[i] +1);
  120.       switch(sw) {
  121.     case '?' :
  122.       HelpMessage(ProgramName, ASPSVersionNumber);
  123.       break;
  124.  
  125.     case 'c' :
  126.       ibuf = atoi(&argv[i][2]);
  127.       if(ibuf <= 0) ibuf = 1;
  128.       psdrv.Copies(ibuf);
  129.       break;
  130.  
  131.     case 'F':
  132.       write_to_file = 1;
  133.       break;
  134.       
  135.     case 'l' :
  136.       psdrv.LandScapeMode();
  137.       break;
  138.  
  139.     case 'm' :
  140.       lr_margin = atof(&argv[i][2]);
  141.       if(lr_margin <= 0)
  142.         lr_margin = DEFAULT_LR_MARGIN;
  143.       psdrv.UseLRMargin();
  144.       break;
  145.  
  146.     case 'M' :
  147.       tb_margin = atof(&argv[i][2]);
  148.       if(tb_margin <= 0)
  149.         tb_margin = DEFAULT_TB_MARGIN;
  150.       psdrv.UseTBMargin();
  151.       break;
  152.  
  153.     case 's' : 
  154.       font_size = atof(&argv[i][2]);
  155.       if(font_size <= 0)
  156.         font_size = DEFAULT_POINT_SIZE;
  157.       break;
  158.     
  159.     case 't' :
  160.       ibuf = atoi(&argv[i][2]);
  161.       if(ibuf < 1) ibuf = DEFAULT_TAB_SIZE;
  162.       psdrv.SetTabStop(ibuf);
  163.       break;
  164.  
  165.     case 'v' :
  166.       VerboseMode = 1;
  167.       break;
  168.  
  169.     case 'f' :
  170.       sbuf = &argv[i][2]; 
  171.       if(strcmp(sbuf, "C") == 0) font_code = PostScriptDrv::COURIER;
  172.       if(strcmp(sbuf, "CB") == 0) font_code = PostScriptDrv::COURIER_BOLD;
  173.       if(strcmp(sbuf, "CO") == 0)
  174.         font_code = PostScriptDrv::COURIER_OBLIQUE;
  175.       if(strcmp(sbuf, "CBO") == 0)
  176.         font_code = PostScriptDrv::COURIER_BOLD_OBLIQUE;
  177.  
  178.       if(strcmp(sbuf, "T") == 0) font_code = PostScriptDrv::TIMES;
  179.       if(strcmp(sbuf, "TB") == 0) font_code = PostScriptDrv::TIMES_BOLD;
  180.       if(strcmp(sbuf, "TI") == 0) font_code = PostScriptDrv::TIMES_ITALIC;
  181.       if(strcmp(sbuf, "TBI") == 0)
  182.         font_code = PostScriptDrv::TIMES_BOLD_ITALIC;
  183.  
  184.       if(strcmp(sbuf, "H") == 0) font_code = PostScriptDrv::HELV;
  185.       if(strcmp(sbuf, "HB") == 0) font_code = PostScriptDrv::HELV_BOLD;
  186.       if(strcmp(sbuf, "HO") == 0) font_code = PostScriptDrv::HELV_OBLIQUE;
  187.       if(strcmp(sbuf, "HBO") == 0)
  188.         font_code = PostScriptDrv::HELV_BOLD_OBLIQUE;
  189.  
  190.       if(strcmp(sbuf, "S") == 0) font_code = PostScriptDrv::SYMBOL;
  191.       break;
  192.       
  193.     case 'p' :
  194.       sbuf = &argv[i][2]; 
  195.       if(strcmp(sbuf, "LET") == 0)
  196.         paper_code = PostScriptDrv::LETTER_SIZE;
  197.       if(strcmp(sbuf, "LEG") == 0)
  198.         paper_code = PostScriptDrv::LEGAL_SIZE;
  199.       if(strcmp(sbuf, "TAB") == 0)
  200.         paper_code = PostScriptDrv::TABLOID_SIZE;
  201.       if(strcmp(sbuf, "A3") == 0)
  202.         paper_code = PostScriptDrv::A3_SIZE;
  203.       if(strcmp(sbuf, "A4") == 0)
  204.         paper_code = PostScriptDrv::A4_SIZE;
  205.       break;
  206.       
  207.     case 'u' :
  208.       man_page = 1;
  209.       cut_lines = 0;
  210.       wrap_lines = 0;
  211.       break;
  212.  
  213.     case 'T' :
  214.       cut_lines = 0;
  215.       break;
  216.  
  217.     case 'W' :
  218.       wrap_lines = 1;
  219.       cut_lines = 0;
  220.       break;
  221.       
  222.     case 'd' :
  223.       duplex = 1;
  224.       break;
  225.  
  226.     case 'D' :
  227.       manualfeed = 1;
  228.       break;
  229.  
  230.     case 'y' :
  231.       use_tray = 1;
  232.       tray_number = atoi(&argv[i][2]);
  233.       break;
  234.  
  235.     case 'h' :
  236.       psdrv.UseHeader();
  237.       break;
  238.       
  239.     case 'H' :
  240.       psdrv.UseHeader();
  241.       psdrv.DrawHeaderLine();
  242.       break;
  243.       
  244.     default:
  245.       cerr << endl;
  246.       cerr << "Unknown switch " << argv[i] << endl;
  247.       cerr << "Exiting..." << endl;
  248.       cerr << endl;
  249.       exit(0);
  250.       break;
  251.       }
  252.     }
  253.   }
  254.  
  255.   return psdrv;
  256. }
  257.  
  258. int GenOutputFileName(char *extension)
  259. // Generate a name for the output file using the open_file
  260. // name with the specified dot extension.
  261. {
  262.   unsigned i = 0;
  263.   for(i = 0; i < MAX_LINE; i++) out_file[i] = '\0';
  264.   char *p = open_file;
  265.   unsigned len = strlen(p);
  266.   for(i = 0; i < len && i != MAX_LINE; i++, p++) {
  267.     if(*p == '.') break;
  268.     out_file[i] = *p;
  269.   }
  270.   if((strlen(out_file) + strlen(extension)) > (MAX_LINE - 1)) return 0;
  271.   strcat(out_file, extension); // Add the file extension (.xxx)
  272.   return 1;
  273. }
  274.  
  275. void DocumentSetup(PostScriptDrv &psdrv)
  276. {
  277.   psdrv.SetPaperSize(paper_code);
  278.   psdrv.SetFont(font_code, font_size);
  279.   if(psdrv.UsingHeader()) {
  280.     psdrv.SetDocumentName(open_file);
  281.     GetSystemTime(time_buf);
  282.     psdrv.SetDateString(time_buf);
  283.   }
  284.   
  285.   psdrv.DocumentSetup(lr_margin, tb_margin, man_page);
  286.   
  287.   if(VerboseMode) {
  288.     cout << "Font            = " << psdrv.TextFont() << endl;
  289.     cout << "Font Size       = " << psdrv.FontSize() << endl;
  290.     cout << "Character width = " << psdrv.CharWidth() << endl;
  291.     cout << "Chars Per inch  = " << psdrv.CharsPerInch() << endl;
  292.     cout << "Columns         = " << psdrv.Columns() << endl;
  293.     
  294.     cout << "Paper size      = ";
  295.     if(paper_code == PostScriptDrv::LETTER_SIZE) cout << "LETTER" << endl;
  296.     if(paper_code == PostScriptDrv::LEGAL_SIZE) cout << "LEGAL" << endl;
  297.     if(paper_code == PostScriptDrv::TABLOID_SIZE) cout << "TABLOID" << endl;
  298.     if(paper_code == PostScriptDrv::A3_SIZE) cout << "A3" << endl;
  299.     if(paper_code == PostScriptDrv::A4_SIZE) cout << "A4" << endl;
  300.     
  301.     cout << "Page width      = " << psdrv.PageWidth() << endl;
  302.     cout << "Page height     = " << psdrv.PageHeight() << endl;
  303.     cout << "Starting X pos  = " << psdrv.StartX() << endl;
  304.     cout << "Starting Y pos  = " << psdrv.StartY() << endl;
  305.     cout << "Lines per page  = " << psdrv.LinesPerPage() << endl;
  306.     
  307.     cout << "Copies          = " << psdrv.NCopies() << endl;    
  308.     cout << "Tab Stop Spaces = " << psdrv.TabStop() << endl;
  309.    
  310.     cout << "Input text file = " << open_file << endl;
  311.     if(write_to_file == 0 ) { 
  312.       cout << "Output PS file  = stdout" << endl;
  313.     }
  314.     else {
  315.       GenOutputFileName(".ps");
  316.       cout << "Output PS file  = " << out_file << endl;
  317.     }
  318.     if(psdrv.GetMode()) cout << "Printing in landscape mode" << endl;
  319.     if(psdrv.UsingLRMargin()) cout << "Using left and right margins" << endl;
  320.     if(psdrv.UsingTBMargin()) cout << "Using top and bottom margins" << endl;
  321.     cout << endl;
  322.     cout << "Press enter to continue..." << endl;
  323.     cin.get();
  324.   }
  325. }
  326.  
  327. int main(int argc, char *argv[])
  328. // NOTE: None of the MSVC compilers will expand wildcard characters
  329. // used in command-line arguments unless linked with the setargv.obj
  330. // library. All the UNIX compliers will expand wildcard characters
  331. // by default.
  332. {
  333.   // If no argument is given print usage message to the screen 1
  334.   if(argc < 2) {
  335.     HelpMessage(ProgramName, ASPSVersionNumber);
  336.     return(0);
  337.   }
  338.  
  339.   // Process command ling arguments and files 
  340.   int narg;
  341.   PostScriptDrv psdrv;
  342.   char *arg = argv[narg = 1];
  343.   while (narg < argc) {
  344.     if (arg[0] != '\0') {
  345.       if (arg[0] == '-') { // Look for command line arguments
  346.     psdrv = ProcessArgs(argc, argv);
  347.       }
  348.       else { 
  349.     open_file = arg; // Update the open file name pointer
  350.     ifstream infile(open_file, ios::in|ios::nocreate);
  351.     if(!infile) {
  352.       cerr << endl;
  353.       cerr << "Cannot open file: " << open_file << endl;
  354.       cerr << "Exiting..." << endl;
  355.       cerr << endl;
  356.       return 0;
  357.     }
  358.     num_files++;
  359.  
  360.     // Process the test file
  361.     DocumentSetup(psdrv);
  362.     if(write_to_file == 0 ) { 
  363.       psdrv.Prologue(cout);
  364.       psdrv.MediaSetup(cout, duplex, manualfeed, use_tray, tray_number);
  365.       
  366.       if(psdrv.ConvertTextFile(infile, cout, wrap_lines, cut_lines) == 0) {
  367.         cerr << endl;
  368.         cerr << "Encountered fatal error during conversion" << endl;
  369.         cerr << "Exiting..." << endl;
  370.         cerr << endl;
  371.         return 1;
  372.       }
  373.       psdrv.Epilogue(cout, psdrv.page_count);
  374.     }
  375.     else {
  376.       GenOutputFileName(".ps");
  377.       ofstream stream(out_file, ios::out); // Open file and truncate it
  378.       if(!stream) {
  379.         cerr << endl;
  380.         cerr << "Cannot write to " << out_file << endl;
  381.         cerr << endl;
  382.         return 1;
  383.       }
  384.       
  385.       psdrv.Prologue(stream);
  386.       psdrv.MediaSetup(stream, duplex, manualfeed, use_tray, tray_number);
  387.       
  388.       if(psdrv.ConvertTextFile(infile, stream, wrap_lines, cut_lines) == 0)
  389.         {
  390.           cerr << endl;
  391.           cerr << "Encountered fatal error during conversion" << endl;
  392.           cerr << "Exiting..." << endl;
  393.           cerr << endl;
  394.           return 1;
  395.         }
  396.       psdrv.Epilogue(stream, psdrv.page_count);
  397.       stream.close();
  398.       infile.close();
  399.     }
  400.       }
  401.       arg = argv[++narg];
  402.     }
  403.   }
  404.   
  405.   if(num_files == 0) {
  406.     cerr << endl;
  407.     cerr << "You must enter a file name." << endl;
  408.     cerr << "Exiting..." << endl;
  409.     cerr << endl;
  410.     return 0;
  411.   }
  412.  
  413.   return 0;
  414. }
  415. // ----------------------------------------------------------- //
  416. // ------------------------------- //
  417. // --------- End of File --------- //
  418. // ------------------------------- //
  419.